package com.cpi.cornpp.purchaseorder.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.form.PurchaseOrderVersionForm;
import com.cpi.cornpp.purchaseorder.service.PurchaseOrderVersionService;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderCostVersionView;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderVersionView;

public class SaveEditPurchaseOrderVersionAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "error";
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_ADD_LABEL = "common.edit.success";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException{
		String[] purchaseOrderCostIds = null;
		String[] purchaseOrderCostAmounts = null;
		PurchaseOrderVersionForm povForm = null;
		PurchaseOrderVersionView povView = null;
		PurchaseOrderCostVersionView pcvView = null;
		PurchaseOrderVersionService povService = null;
		List pcvListView = null;
		HttpSession session = null;
		ActionForward forward = null;
		String messageError;
		MessageResources messageResources = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		session = request.getSession(false);
		
		try{
			messageResources = getResources(request);
			if (form != null){
				povView = new PurchaseOrderVersionView(); 
				povService = new PurchaseOrderVersionService();
				povForm = (PurchaseOrderVersionForm) form;
				purchaseOrderCostIds = povForm.getPurchaseOrderVersionCostIds();
				purchaseOrderCostAmounts = povForm.getPurchaseOrderVersionCostAmounts();
				
				povView.setPurchaseOrderVersionId(new Integer(povForm.getPurchaseOrderVersionId()));
				if(!StringUtils.isEmpty(povForm.getPurchaseOrderVersionTransportId())){
					povView.setTransportId(new Integer(povForm.getPurchaseOrderVersionTransportId()));
				}
				povView.setBiWeekPeriodVersionId(new Integer(povForm.getPurchaseOrderVersionPurchaseDate()));
				povView.setPlantId(!(povForm.getPurchaseOrderVersionPlantId().equalsIgnoreCase("-1")) ? new Integer(povForm.getPurchaseOrderVersionPlantId()) : null);
				povView.setCode(povForm.getPurchaseOrderVersionCode());
				povView.setPostFix(povForm.getPurchaseOrderVersionCodeNumber());
				povView.setCapacity(new Double(povForm.getPurchaseOrderVersionCapacity()));
				povView.setCornTypeId(povForm.getCornTypeId());
				povView.setPurchaseOrderId(new Integer(povForm.getPurchaseOrderId()));
				if (((purchaseOrderCostIds != null) && (purchaseOrderCostAmounts != null)) && (purchaseOrderCostIds.length == purchaseOrderCostAmounts.length)){
					pcvListView = new ArrayList(purchaseOrderCostIds.length);
					
					for (int x = 0; x < purchaseOrderCostIds.length; x++){
						pcvView = new PurchaseOrderCostVersionView();
						
						pcvView.setPurchaseOrderVersionId(povView.getPurchaseOrderVersionId());
						pcvView.setCostElementId(new Integer(purchaseOrderCostIds[x]));
						pcvView.setAmount(new Double(purchaseOrderCostAmounts[x]));
						pcvListView.add(pcvView);
					}
					
					povView.setPurchaseOrderCostVersions(pcvListView);
				}
				
				povService.updatePurchaseOrderVersion(povView);
				messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
				session.setAttribute(MESSAGE, messageError);

				forward = mapping.findForward(SUCCESS_FORWARD);
			}
			
		} catch(PurchaseProgramException ppe){
			session.setAttribute(CUSTOM_EXCEPTION, ppe);
			throw ppe;
		} catch(Exception e){
			PurchaseProgramException ppe=null;
			e.printStackTrace(System.out);
			ppe= new PurchaseProgramException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, ppe);
			throw ppe;
		}
		
		return forward;
	}
}